package com.innovation.chengzhangriji.ui.widget;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.innovation.chengzhangriji.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class BabyAllViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OnClickHeadListener {
        void onClickHead(BabyAllViewHolder holder);
    }

    private OnClickHeadListener onClickHeadListener;

    private RoundedImageView imageHead;

    private ImageView imageCreate;

    private ImageView imageAdmin;

    private CuteTextView mTextName;

    private DisplayImageOptions options;

    public BabyAllViewHolder(View itemView) {
        super(itemView);
        imageHead = (RoundedImageView) itemView.findViewById(R.id.image_head);
        imageHead.setOnClickListener(this);
        imageCreate = (ImageView) itemView.findViewById(R.id.image_create);
        imageAdmin = (ImageView) itemView.findViewById(R.id.image_admin);
        mTextName = (CuteTextView) itemView.findViewById(R.id.text_name);
        setOptions();
    }

    @Override
    public void onClick(View v) {
        if (onClickHeadListener == null) {
            return;
        }
        if (v.getId() == R.id.image_head) {
            onClickHeadListener.onClickHead(this);
        }
    }

    public void setOnClickHeadListener(OnClickHeadListener onClickHeadListener) {
        this.onClickHeadListener = onClickHeadListener;
    }

    public void setImageHeadUrl(String url) {
        ImageLoader.getInstance().displayImage(url, imageHead, options);
    }

    public void setImageCreate(boolean create) {
        if (create) {
            imageCreate.setVisibility(View.VISIBLE);
        } else {
            imageCreate.setVisibility(View.GONE);
        }
    }

    public void setImageAdmin(boolean admin, Drawable drawable) {
        if (admin) {
            imageAdmin.setVisibility(View.VISIBLE);
            imageAdmin.setImageDrawable(drawable);
        } else {
            imageAdmin.setVisibility(View.GONE);
        }
    }

    public void setName(String name) {
        mTextName.setText(name);
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_head_circle_default_small)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_head_circle_default_small)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_head_circle_default_small)  //image加载失败
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
